package com.lingo.ffmpegdemo.lg.utils;

import cn.hutool.core.lang.Console;
import com.google.common.util.concurrent.ThreadFactoryBuilder;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Arrays;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author watson
 * @date 2021/5/25 17:18
 * @Description: 循环修改文件名称并复制到同一文件夹
 **/
public class UpdateMyFile {
    private static Integer POLL_SIZE = 100;
    private static Integer MAX_SIZE = 100;
    private static Integer KEEP_ALIVE_TIME = 10;
    public static ThreadPoolExecutor executorService = new ThreadPoolExecutor(
            POLL_SIZE, MAX_SIZE,
            KEEP_ALIVE_TIME, TimeUnit.HOURS,
            new ArrayBlockingQueue<>(100),
            new ThreadFactoryBuilder()
                    .setNameFormat("fsd-pool-%d").build());

    public static final String OUT_PATH = "D:\\ffmpegMedia\\temp";
    public static int total = 0;
//    public static ExecutorService threadPool = Executors.newFixedThreadPool(12);


    public static void updateFile(String filePath) {

        File file = new File(filePath);
        String fileName = file.getName();
        System.out.println("filename" + fileName);
        String[] list = file.list();
        try {
            FileInputStream fileInputStream = null;
            FileOutputStream fileOutputStream = null;
            for (int i = 0; i < list.length; i++) {
                String strPath = filePath + "\\" + list[i];
                File file1 = new File(strPath);
                if (file1.isDirectory()) {
                    String[] list1 = file1.list();
                    Arrays.sort(list1);
                    //遍历二级目录
                    for (int k = 0; k < list1.length; k++) {
                        String strPath1 = strPath + "\\" + list1[k];
                        File file2 = new File(strPath1);
                        String name = file2.getName();
                        if (name.endsWith("mp4") || name.endsWith("avi")) {
                            total++;
                            CompletableFuture.runAsync(() -> {
                                System.out.println(total + "==================" + Thread.currentThread().getId());
                                try {
                                    int num = total;
                                    copy(fileInputStream, fileOutputStream, strPath, name, num);
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }, executorService);
                        }
                    }
                } else {
                    String name = file1.getName();
                    if (name.endsWith("mp4") || name.endsWith("avi")) {
                        total++;

                        CompletableFuture<Void> completableFuture = CompletableFuture.runAsync(() -> {
                            System.out.println(total + "==============" + Thread.currentThread().getId());
                            try {
                                int num = total;
                                copy(fileInputStream, fileOutputStream, strPath, name, num);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }, executorService);
                    }
                }

            }
            if (fileInputStream != null) {
                fileInputStream.close();//释放资源
                fileOutputStream.close();//释放资源
            }

        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    public static void copy(FileInputStream fileInputStream,
                            FileOutputStream fileOutputStream,
                            String strPath,
                            String name,
                            int num) throws Exception {
        fileInputStream = new FileInputStream(strPath);

        String outFile = OUT_PATH + "\\" + num + "__" + name;
        System.out.println(outFile);
        fileOutputStream = new FileOutputStream(outFile);
        byte[] bytes = new byte[1024 * 10];
        int len = 0;
        while ((len = fileInputStream.read(bytes)) != -1) {
            fileOutputStream.write(bytes, 0, len);
        }
    }

    public static void getFile(String firstPath) {
        //总目录
        File file = new File(firstPath);
        String[] list = file.list();
        Arrays.sort(list);
        File file1 = null;
        for (int i = 0; i < list.length; i++) {
            String filePath = firstPath + "\\" + list[i];
            file1 = new File(filePath);
            if (file1.isDirectory()) {
                updateFile(filePath);
            }
        }
        Console.log("修改完毕");
    }


    public static void main(String[] args) {
        Console.log("============begin=========");
//        updateFile("D:\\FFOutput\\07泰勒公式");
        getFile("F:\\网盘下载\\hadoop\\北风网.Python零基础人工智能就业视频教程");
        Console.log("============end=========");
    }


}
